/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com;

import java.util.HashMap;
import java.util.Map;

/**
 * @author THINK
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2016年5月27日 下午2:16:15
 * @Title <B>todo</B>
 * 
 *        <pre>
 * todo
 * </pre>
 */

public class SingleDemo {
	public static void main(String args[]) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException {
		System.out.println(Son.getInstance().getName());
		 //System.out.println("我是谁");
	}
}

class Son extends Father {
	private String name = "儿子";
	final String CLASS = "demo";

	protected String getName() {
		return this.query("aaa");
	}

	public static Son getInstance() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException {
		// 这里必须是全局路径 否则无法找到
		return (Son) instance("com.Son");
	}
}

class Father {
	private static Map<String, Object> instance = new HashMap<String, Object>();
	private String name = "父类";

	protected void Fatcher() {
		System.out.println("我是父类");
	}

	protected String query(String sql) {
		return sql + "has been done";
	}

	public static Object instance(String objname)
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		if (instance.get(objname) == null
				|| !(instance.get(objname) instanceof Father)) {
			instance.put(objname, Class.forName(objname).newInstance());
		}
		return instance.get(objname);
	}
}